package com.java.huxb.backend.hall.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.java.huxb.backend.hall.controller.vo.HallSavedReqVO;
import com.java.huxb.backend.hall.controller.vo.HallsReqVO;
import com.java.huxb.backend.hall.controller.vo.HallsRespVO;
import com.java.huxb.backend.hall.dao.mapper.MoocFieldTMapper;
import com.java.huxb.backend.hall.dao.mapper.MoocHallFilmInfoTMapper;
import com.java.huxb.backend.hall.feignApi.FilmFeignComsumer;
import com.java.huxb.backend.hall.service.HallsServiceAPI;
import com.java.huxb.backend.utils.vo.BaseResponseVO;
import com.java.huxn.backend.api.film.vo.DescribeFilmRespVO;
import com.netflix.discovery.converters.Auto;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author huxb
 * @date 2020/1/30 0030 上午 9:31
 */
@Service
public class HallsServiceImpl implements HallsServiceAPI {

    @Resource
    private MoocFieldTMapper fieldTMapper;

    @Resource
    private MoocHallFilmInfoTMapper filmInfoTMapper;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Resource
    private FilmFeignComsumer filmFeignComsumer;

    @Override
    public IPage<HallsRespVO> findValidPage(HallsReqVO hallsReqVO) {
        Page<HallsReqVO> page = new Page<>(hallsReqVO.getNowPage(), hallsReqVO.getPageSize());

        QueryWrapper queryWrapper = new QueryWrapper();
        if (hallsReqVO.getCinemaId() != null) {
            queryWrapper.eq("cinema_id", hallsReqVO.getCinemaId());
        }
        return fieldTMapper.findHallPage(page, queryWrapper);
    }

    @Override
    public void addHall(HallSavedReqVO hallSavedReqVO) {

    }

    public BaseResponseVO findFilmDetailByIdWithFeign(String filmId) {
       return  filmFeignComsumer.findValidFilmDetailById(filmId);
    }

    public  String feignTest() {
        String s = filmFeignComsumer.feignTest();
        return s + " test";
    }

    public Object findFilmDetailById(String filmId) {
        ServiceInstance choose = loadBalancerClient.choose("film-service");
        String host = choose.getHost();
        int port = choose.getPort();

        String uri = "/films/" + filmId;
        String url = "http://" + host + ":" + port + uri;

        return restTemplate.getForObject(url, JSONObject.class);
    }
}
